﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using Hi_Baidu.Model.ProcessModel;
using Hi_Biadu.UI.PersonalObject;

namespace Hi_Baidu.Model.ViewModel
{
    public class IOViewModel : Model
    {
        /// <summary>
        ///     在指定的树状图中初始化加载当前计算机的硬盘驱动器列表
        /// </summary>
        /// <param name="treeView" type="System.Windows.Controls.TreeView">
        ///     <para>
        ///         要加载到的树状图
        ///     </para>
        /// </param>
        /// <param name="driveInfo" type="System.IO.DriveInfo[]">
        ///     <para>
        ///         硬盘驱动器列表
        ///     </para>
        /// </param>
        /// <param name="DiskImage" type="Image">
        ///     <para>
        ///         硬盘驱动器图标
        ///     </para>
        /// </param>
        public static void InitDrive(TreeView treeView, DriveInfo[] driveInfo, Image DiskImage)
        {
            foreach (DriveInfo drive in driveInfo)
            {
                if(drive.IsReady)
                {
                    CheckBox Ckb_Select = new CheckBox();
                    Ckb_Select.VerticalAlignment = VerticalAlignment.Bottom;
                    Ckb_Select.Margin = new Thickness(0, 0, 5, 0);
                    Image img = new Image();
                    img.Width = 32;
                    img.Height = 32;
                    img.Source = DiskImage.Source;
                    img.Margin = new Thickness(0, 0, 3, 0);
                    StackPanel SP_Panel = new StackPanel();
                    SP_Panel.Orientation = Orientation.Horizontal;
                    SP_Panel.Tag = drive.Name;
                    TextBlock TB_DiskName = new TextBlock();
                    TB_DiskName.Margin = new Thickness(5, 0, 0, 0);
                    TB_DiskName.VerticalAlignment = VerticalAlignment.Bottom;
                    TB_DiskName.Foreground = Brushes.White;
                    TB_DiskName.Text = "硬盘驱动器 " + drive.Name.Replace("\\","");
                    SP_Panel.Children.Add(Ckb_Select);
                    SP_Panel.Children.Add(img);
                    SP_Panel.Children.Add(TB_DiskName);
                    IOTreeViewItem treeViewItem = new IOTreeViewItem();
                    treeViewItem.Header = SP_Panel;
                    treeViewItem.ObjType = IOTreeViewItem.ObjectType.DISK;
                    treeViewItem.Path = drive.Name;
                    treeView.Items.Add(treeViewItem);
                }
            }
        }

        public static void SetDirectory(IOTreeViewItem treeViewItem, String[] Directorys, Image FolderImage)
        {
            foreach (String dir in Directorys)
            {
                CheckBox Ckb_Select = new CheckBox(); 
                Ckb_Select.VerticalAlignment = VerticalAlignment.Bottom;
                Ckb_Select.Margin = new Thickness(0, 0, 5, 0);
                Image img = new Image();
                img.Width = 32;
                img.Height = 32;
                img.Source = FolderImage.Source;
                StackPanel SP_Panel = new StackPanel();
                SP_Panel.Orientation = Orientation.Horizontal;
                SP_Panel.Tag = dir + "/";
                TextBlock TB_FolderName = new TextBlock();
                TB_FolderName.Margin = new Thickness(5, 0, 0, 0);
                TB_FolderName.VerticalAlignment = VerticalAlignment.Bottom;
                TB_FolderName.Foreground = Brushes.White;
                TB_FolderName.Text = dir;
                SP_Panel.Children.Add(Ckb_Select);
                SP_Panel.Children.Add(img);
                SP_Panel.Children.Add(TB_FolderName);
                IOTreeViewItem treeviewitem = new IOTreeViewItem();
                treeviewitem.Margin = new Thickness(0, 5, 0, 0);
                treeviewitem.Header = SP_Panel;
                treeviewitem.ObjType = IOTreeViewItem.ObjectType.DIRECTORY;
                treeviewitem.Path = dir + "/";
                treeViewItem.Items.Add(treeviewitem);
            }
        }

        public static void SetFile(IOTreeViewItem treeViewItem, String[] Files)
        {
            ImageSourceConverter isc = new ImageSourceConverter();
            foreach (String file in Files)
            {
                String[] FileData = file.Split(new String[] { "." }, StringSplitOptions.RemoveEmptyEntries);
                CheckBox Ckb_Select = new CheckBox();
                Ckb_Select.VerticalAlignment = VerticalAlignment.Bottom;
                Ckb_Select.Margin = new Thickness(0, 0, 5, 0);
                Image img = new Image();
                img.Width = 32;
                img.Height = 32;
                BitmapImage BImage = IOProcessModel.ConvertToWPF(IOProcessModel.GetIconByFileType("." + FileData[FileData.Length - 1], true));
                img.Source = BImage;
                StackPanel SP_Panel = new StackPanel();
                SP_Panel.Orientation = Orientation.Horizontal;
                SP_Panel.Tag = file;
                TextBlock TB_FolderName = new TextBlock();
                TB_FolderName.Margin = new Thickness(5, 0, 0, 0);
                TB_FolderName.VerticalAlignment = VerticalAlignment.Bottom;
                TB_FolderName.Foreground = Brushes.White;
                TB_FolderName.Text = file;
                SP_Panel.Children.Add(Ckb_Select);
                SP_Panel.Children.Add(img);
                SP_Panel.Children.Add(TB_FolderName);
                IOTreeViewItem treeviewitem = new IOTreeViewItem();
                treeviewitem.Margin = new Thickness(0, 5, 0, 0);
                treeviewitem.Header = SP_Panel;
                treeviewitem.ObjType = IOTreeViewItem.ObjectType.FILE;
                treeviewitem.Path = file;
                treeviewitem.Ext = FileData[FileData.Length - 1];
                treeviewitem.OwnerImage = BImage;
                treeViewItem.Items.Add(treeviewitem);
            }
        }

    }
}
